package com.xbhog.screwdemo.聚合根;

import java.util.ArrayList;

import java.util.List;

public class OrderAggregate extends BaseEntity {
    private List<OrderItem> orderItems;
    private String customerName;
    private boolean isPaid;

    public OrderAggregate(String customerName) {
        this.customerName = customerName;
        this.orderItems = new ArrayList<>();
        this.isPaid = false;
    }

    public void addItem(OrderItem item) {
        //业务规则：订单未支付时才能添加订单项
        if (!isPaid) {
            orderItems.add(item);
        } else {
            throw new IllegalStateException("Can not add items to apaid order.");
        }
    }

    public double getTotalAmount() {
        return orderItems.stream().mapToDouble(OrderItem::getTotalPrice).sum();
    }

    public void markAsPaid() {
        //业务规则：订单总金额必须大于0才能标记为已支付
        if (getTotalAmount() > 0) {
            isPaid = true;
        } else {
            throw new IllegalStateException("Order total must begreater than 0 to bepaid.");
        }
    }
}
